{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python385jvsc74a57bd0ae7890921ac3c17143ff000ac7152addc6614e2051824da39aa37dab63d26d82",
   "display_name": "Python 3.8.5 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "numpy能够帮助我们处理数值，但是pandas除了处理数值之外(基于numpy)，还能够帮助我们处理其他类型的数据 Series 一维，DataFrame 二维\n",
    "Series是一种类似于一维数组的对象，由一组数据和一组与之相关的数据标签(索引)组成"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0    4\n",
       "1    5\n",
       "2    6\n",
       "3    7\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 59
    }
   ],
   "source": [
    "#Series的创建01\n",
    "pd.Series([4,5,6,7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a    4\n",
       "b    5\n",
       "c    6\n",
       "d    7\n",
       "e    8\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 60
    }
   ],
   "source": [
    "#Series的创建02——自定义索引\n",
    "pd.Series([4,5,6,7,8],index=['a','b','c','d','e'])   #index索引是用[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 61
    }
   ],
   "source": [
    "#Series的创建03——传入字典格式数据，字典的key当成指定索引\n",
    "pd.Series({\"a\":1,\"b\":2}) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a    0\n",
       "b    0\n",
       "c    0\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 62
    }
   ],
   "source": [
    "#Series的创建03——创建一个值都是0的数组\n",
    "pd.Series(0,index=['a','b','c'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 63
    }
   ],
   "source": [
    "#Series的增加数据——直接增加\n",
    "# Series的add()方法是加法计算不是增加Series元素用的。\n",
    "# 使用append连接其他Series\n",
    "s1=pd.Series([1,2,3],index=['a','b','c'])\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "e    5\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 64
    }
   ],
   "source": [
    "s1['e']=5\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "e    5\n",
       "d    4\n",
       "e    5\n",
       "f    6\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 65
    }
   ],
   "source": [
    "#Series的增加数据——增加Series\n",
    "s2=pd.Series([4,5,6],index=['d','e','f'])\n",
    "s1=s1.append(s2)\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a    1\n",
       "c    3\n",
       "e    5\n",
       "d    4\n",
       "e    5\n",
       "f    6\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 66
    }
   ],
   "source": [
    "#Series的删除数据\n",
    "s1.drop('b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    9\n",
       "e    5\n",
       "d    4\n",
       "e    5\n",
       "f    6\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 67
    }
   ],
   "source": [
    "#Series的修改数据\n",
    "s1['c']=9\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "metadata": {},
     "execution_count": 68
    }
   ],
   "source": [
    "#Series的查询数据——通过对象直接选取\n",
    "s1.a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "metadata": {},
     "execution_count": 69
    }
   ],
   "source": [
    "#Series的查询数据——通过显式索引选取单值\n",
    "s1['b']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 70
    }
   ],
   "source": [
    "#Series的查询数据——通过显式索引选取多值\n",
    "s1[['a','b']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "c    9\n",
       "e    5\n",
       "e    5\n",
       "f    6\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 71
    }
   ],
   "source": [
    "#Series的查询数据——通过显式索引通过布尔类型索引筛选\n",
    "s1[s1>4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "metadata": {},
     "execution_count": 72
    }
   ],
   "source": [
    "#Series的查询数据——通过隐式索引选取单值\n",
    "s1[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "c    9\n",
       "e    5\n",
       "d    4\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 73
    }
   ],
   "source": [
    "#Series的查询数据——通过隐式索引选取多值\n",
    "s1[[2,3,4]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "b    2\n",
       "e    5\n",
       "e    5\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 74
    }
   ],
   "source": [
    "#Series的查询数据——通过步数选取\n",
    "s1[1:7:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tom      23\n",
       "lucy     95\n",
       "bobo     47\n",
       "jay      13\n",
       "helen    89\n",
       "Name: scored, dtype: int32"
      ]
     },
     "metadata": {},
     "execution_count": 75
    }
   ],
   "source": [
    "#Series的切片数据\n",
    "b = pd.Series(data=np.random.randint(0,100,size=(5,)),index=['tom','lucy','bobo','jay','helen'],name='scored')\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a    31\n",
       "b    46\n",
       "c    22\n",
       "d    24\n",
       "e    35\n",
       "f    40\n",
       "g    42\n",
       "h    34\n",
       "i    30\n",
       "j    46\n",
       "dtype: int32"
      ]
     },
     "metadata": {},
     "execution_count": 76
    }
   ],
   "source": [
    "c = pd.Series(data=(np.random.randint(20,50,size=10)),index=list('abcdefghij'))\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "23"
      ]
     },
     "metadata": {},
     "execution_count": 77
    }
   ],
   "source": [
    "#loc——使得切片和取值操作都是依照显式索引进行\n",
    "b.loc['tom']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tom      23\n",
       "lucy     95\n",
       "bobo     47\n",
       "jay      13\n",
       "helen    89\n",
       "Name: scored, dtype: int32"
      ]
     },
     "metadata": {},
     "execution_count": 78
    }
   ],
   "source": [
    "b.loc['tom':]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "tom     23\n",
       "lucy    95\n",
       "bobo    47\n",
       "Name: scored, dtype: int32"
      ]
     },
     "metadata": {},
     "execution_count": 79
    }
   ],
   "source": [
    "#iloc——使得切片和取值操作都是依照隐式索引进行——取多行\n",
    "b[0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a    31\n",
       "c    22\n",
       "e    35\n",
       "g    42\n",
       "dtype: int32"
      ]
     },
     "metadata": {},
     "execution_count": 80
    }
   ],
   "source": [
    "#iloc——使得切片和取值操作都是依照隐式索引进行——根据步长切片\n",
    "c.iloc[0:8:2]"
   ]
  },
  {
   "source": [
    "##缺失数据的处理（******）\n",
    "# dropna() # 过滤掉值为NaN的行\n",
    "# fillna() # 填充缺失数据\n",
    "# isnull() # 返回布尔数组，缺失值对应为True\n",
    "# notnull() # 返回布尔数组，缺失值对应为False"
   ],
   "cell_type": "code",
   "metadata": {},
   "execution_count": 81,
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "1     hello\n",
       "2    python\n",
       "3    pandas\n",
       "dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 82
    }
   ],
   "source": [
    "#创建一个含有NAN的数据\n",
    "st = {1:'hello',2:'python',3:'pandas'}\n",
    "obj = pd.Series(st)\n",
    "obj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "1     hello\n",
       "2    python\n",
       "3    pandas\n",
       "4       NaN\n",
       "dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 83
    }
   ],
   "source": [
    "a = {1,2,3,4}#定义一个索引变量\n",
    "st1=pd.Series(st,index=a) #将第二步定义的a变量作为索引传入\n",
    "st1 #因为4没有出现在st的键中，所以返回的是缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "#dropna() ——过滤掉值有NaN的行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "1     hello\n",
       "2    python\n",
       "3    pandas\n",
       "dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 86
    }
   ],
   "source": [
    "st1.dropna() #浅删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "1     hello\n",
       "2    python\n",
       "3    pandas\n",
       "4     numpy\n",
       "dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 88
    }
   ],
   "source": [
    "#fillna()——填充缺失数据，这个填写numpy\n",
    "st1.fillna('numpy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "1    False\n",
       "2    False\n",
       "3    False\n",
       "4     True\n",
       "dtype: bool"
      ]
     },
     "metadata": {},
     "execution_count": 89
    }
   ],
   "source": [
    "#isnull()——缺失值返回True\n",
    "st1.isnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "1     True\n",
       "2     True\n",
       "3     True\n",
       "4    False\n",
       "dtype: bool"
      ]
     },
     "metadata": {},
     "execution_count": 90
    }
   ],
   "source": [
    "# notnull()——不是缺失值返回True\n",
    "st1.notnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "1     hello\n",
       "2    python\n",
       "3    pandas\n",
       "dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 91
    }
   ],
   "source": [
    "#利用布尔值索引过滤缺失值\n",
    "st1[st1.notnull()] #只有True的才显示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a    33.0\n",
       "b    14.0\n",
       "c    32.0\n",
       "d    45.0\n",
       "dtype: float64"
      ]
     },
     "metadata": {},
     "execution_count": 92
    }
   ],
   "source": [
    "sr1 = pd.Series([12,23,34], index=['c','a','d'])\n",
    "sr3 = pd.Series([11,20,10,14], index=['d','c','a','b'])\n",
    "sr1.add(sr3,fill_value=0)# 将缺失值设为0，所以最后算出来b索引对应的结果为14"
   ]
  }
 ]
}